package lxx.pandams.adapter;

import java.util.List;


import lxx.pandams.R;
import lxx.pandams.bean.BlackNumberInfo;
import lxx.pandams.dao.CommSafeDao;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CommSafeAdapter extends BaseAdapter{
	
	private Context ctx;
	private List<BlackNumberInfo> list;
	
	public CommSafeAdapter(Context ctx,List<BlackNumberInfo> list){
		this.ctx = ctx;
		this.list = list;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(convertView == null){
			convertView = View.inflate(ctx, R.layout.listview_blacknumber_item, null);
			//2,减少findViewById()次数
			//复用viewHolder步骤三
			holder = new ViewHolder();
			//复用viewHolder步骤四
			holder.tv_phone = (TextView) convertView.findViewById(R.id.tv_phone);
			holder.tv_mode = (TextView)convertView.findViewById(R.id.tv_mode);
			holder.iv_delete = (ImageView)convertView.findViewById(R.id.iv_delete);
			//复用viewHolder步骤五
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		
		holder.iv_delete.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				//1,数据库删除
				CommSafeDao.getInstance(ctx).delete(list.get(position).phone);
				//2,集合中的删除
				list.remove(position);
				//3,通知数据适配器刷新
				
			}
		});
		
		holder.tv_phone.setText(list.get(position).phone);
		int mode = Integer.parseInt(list.get(position).mode);
		switch (mode) {
		case 1:
			holder.tv_mode.setText("拦截短信");
			break;
		case 2:
			holder.tv_mode.setText("拦截电话");
			break;
		case 3:
			holder.tv_mode.setText("拦截所有");
			break;
		}
		return convertView;
	}
	
	static class ViewHolder{
		TextView tv_phone;
		TextView tv_mode;
		ImageView iv_delete;
	}

}
